package lv.ante.dziesmas;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SVGParser {

	private String filePath;

	private String viewBox;
	private String pieceBottom;
	private String markupBottom;
	private String pieceLeft;
	private String opusLeft;
	
	private String rectString; 
	private String svgString;

	public SVGParser(String filePath) {
		this.filePath = filePath;
	}

	public void parse() throws Exception {
		String content = getFileAsString(filePath);
		Pattern rootPattern = Pattern
				.compile("<svg[^<>]+viewBox=\"([0-9 \\.]+)\">");
		Matcher rootMatcher = rootPattern.matcher(content);
		rootMatcher.find();
		viewBox = rootMatcher.group(1);

		Pattern transformPattern = Pattern
				.compile("<text transform=\"translate\\(([0-9 \\.,]+)\\)[^<>]+>\\s*<tspan>\\s*(__|dziesmas\\.ante\\.lv)\\s*</tspan>\\s*</text>");
		Matcher transformMatcher = transformPattern.matcher(content);
		transformMatcher.find();
		String s1 = transformMatcher.group(1);
		String[] sa1 = s1.split(",\\s*");
		opusLeft = sa1[0];
		pieceBottom = sa1[1];
		transformMatcher.find();
		String s2 = transformMatcher.group(1);
		String[] sa2 = s2.split(",\\s*");
		pieceLeft = sa2[0];
		transformMatcher.find();
		String s3 = transformMatcher.group(1);
		String[] sa3 = s3.split(",\\s*");
		markupBottom = sa3[1];
	}

	public void replaceRect(String fileName) throws InterruptedException, IOException {
		
		ImageSize imageSize = new ImageSize("../images/" + fileName + ".png");
		
		BoundingBox box = new BoundingBox(viewBox, pieceBottom, markupBottom,
				pieceLeft, opusLeft, imageSize.getWidthPx(), imageSize.getHeightPx());
		rectString = box.toRectString();
		svgString = box.toString();
		
		FileInputStream fis = new FileInputStream(fileName + ".svg");
		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
		BufferedReader br = new BufferedReader(isr);
		
		FileOutputStream fos = new FileOutputStream("../svg/" + fileName + ".svg");
		OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
		BufferedWriter bw = new BufferedWriter(osw);
		
		String line = null;
		int count = 0; 
		while ((line = br.readLine()) != null) {
//			bw.write(line);
			if (count == 0) {
				bw.write(svgString);
			}
			else {
				bw.write(line);
			}
			bw.newLine();			
			count++;
		}
		bw.close();
		osw.close();
		fos.close();
		
		br.close();
		isr.close();
		fis.close();
		

	}

	public static String getFileAsString(String path) throws IOException {
		StringBuffer result = new StringBuffer();
		FileInputStream fis = new FileInputStream(path);
		InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while ((line = br.readLine()) != null) {
			result.append(line);
			result.append(System.getProperty("line.separator"));
		}
		br.close();
		isr.close();
		fis.close();
		return result.toString();
	}

}
